package com.adams.custom.page.mgt.dao.impl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.adams.access.db.bean.ViewEvent;
import com.adams.access.db.bean.ViewPanel;
import com.adams.access.db.bean.ViewWin;
import com.adams.access.db.dao.hibernate.HibernateGenericDao;
import com.adams.custom.page.util.ViewCacheUtil;

/**
 * 
 * File Name : ViewEventDaoImpl.java
 * 
 * @Description : 页面窗口实体事件访问实现类
 */
public class ViewEventDaoImpl extends HibernateGenericDao<ViewEvent, Long>
{
	private static String cacheMapByPk_key = "ViewEvent_cacheMapByPk";

	private static String cacheMapByViewpanelid_key = "ViewEvent_cacheMapByViewpanelid";

	private static String cacheMapByViewwinid_key = "ViewEvent_cacheMapByViewwinid";

	private static List<ViewEvent> ZONELIST = new ArrayList<ViewEvent>();

	/**
	 * Description : 页面面板实体数据访问实现类 Author : 904370
	 */
	private ViewPanelDaoImpl viewPanelDaoImpl;

	/**
	 * Description : 页面窗口实体数据访问实现类 Author : 904370
	 */
	private ViewWinDaoImpl viewWinDaoImpl;

	public void setViewPanelDaoImpl(ViewPanelDaoImpl viewPanelDaoImpl)
	{
		this.viewPanelDaoImpl = viewPanelDaoImpl;
	}

	public void setViewWinDaoImpl(ViewWinDaoImpl viewWinDaoImpl)
	{
		this.viewWinDaoImpl = viewWinDaoImpl;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#save(java
	 * .lang.Object)
	 */
	public void save(ViewEvent model)
	{
		super.save(model);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.adams.access.db.dao.GenericDao#get(java.io.Serializable)
	 */
	@SuppressWarnings("unchecked")
	@Override
	public ViewEvent get(Serializable id)
	{
		String key = id + "";
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (!cacheMapByPk.containsKey(key))
		{
			synchronizationCache();
		}
		return (ViewEvent) cacheMapByPk.get(key);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#delete(java
	 * .lang.Object)
	 */
	public void delete(ViewEvent model)
	{
		super.delete(model);
	}

	/**
	 * Description : 删除ViewEvent列表
	 * 
	 * @param models
	 * 
	 */
	public void deleteAll(List<ViewEvent> models)
	{
		for (ViewEvent model : models)
		{
			super.delete(model);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.adams.access.db.dao.hibernate.HibernateGenericDao#saveOrUpdate
	 * (java.lang.Object)
	 */
	public void saveOrUpdate(ViewEvent model)
	{
		super.saveOrUpdate(model);
	}

	/**
	 * Description : 根据面板id得到ViewEvent列表
	 * 
	 * @param viewpanelid
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewEvent> findByViewpanelid(Long viewpanelid)
	{
		String key = viewpanelid + "";
		Map cacheMapByViewpanelid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewpanelid_key);
		if (!cacheMapByViewpanelid.containsKey(key))
		{
			synchronizationCache();
		}
		List<ViewEvent> result = new ArrayList<ViewEvent>();
		List<ViewEvent> temp = (List<ViewEvent>) cacheMapByViewpanelid.get(key);
		if (null != temp && temp.size() > 0)
		{
			result.addAll(temp);
			// sort(result);
		}
		return result;
	}

	/**
	 * Description : 根据窗口id得到ViewEvent列表
	 * 
	 * @param viewpanelid
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewEvent> findByViewwinid(Long viewwinid)
	{
		String key = viewwinid + "";
		Map cacheMapByViewwinid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewwinid_key);
		if (!cacheMapByViewwinid.containsKey(key))
		{
			synchronizationCache();
		}
		List<ViewEvent> result = new ArrayList<ViewEvent>();
		List<ViewEvent> temp = (List<ViewEvent>) cacheMapByViewwinid.get(key);
		if (null != temp && temp.size() > 0)
		{
			result.addAll(temp);
			// sort(result);
		}
		return result;
	}

	private void sort(List<ViewEvent> result)
	{
		if (null == result || result.size() == 0)
		{
			return;
		}
		Collections.sort(result, new Comparator<ViewEvent>()
		{
			public int compare(ViewEvent o1, ViewEvent o2)
			{
				if (o1.getOrderid() > o2.getOrderid())
				{
					return 1;
				} else if (o1.getOrderid() == o2.getOrderid())
				{
					if (o1.getId() > o2.getId())
					{
						return 1;
					} else if (o1.getId() == o2.getId())
					{
						return 0;
					}
					return -1;
				}
				return -1;
			}
		});
	}

	/**
	 * Description : 得到所有ViewEvent对象
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<ViewEvent> findAll()
	{
		List<ViewEvent> result = null;
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		if (cacheMapByPk.size() == 0)
		{
			result = synchronizationCache();
			return result;
		} else
		{
			result = new ArrayList<ViewEvent>();
			Set keySet = cacheMapByPk.keySet();
			for (Iterator it = keySet.iterator(); it.hasNext();)
			{
				Object key = it.next();
				ViewEvent temp = (ViewEvent) cacheMapByPk.get(key);
				if (null != temp)
				{
					result.add(temp);
				}
			}
		}
		sort(result);
		return result;
	}

	private List<ViewEvent> findAllFromDB()
	{
		List<ViewEvent> result = query("from ViewEvent");
		return result;
	}

	/**
	 * Description : 同步缓存
	 * 
	 * @return
	 * 
	 */
	@SuppressWarnings("unchecked")
	public synchronized List<ViewEvent> synchronizationCache()
	{
		List<ViewEvent> result = findAllFromDB();
		Map cacheMapByPk = ViewCacheUtil.getConfigDataCacheMap(cacheMapByPk_key);
		Map cacheMapByViewpanelid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewpanelid_key);
		cacheMapByViewpanelid.clear();

		Map cacheMapByViewwinid = ViewCacheUtil
				.getConfigDataCacheMap(cacheMapByViewwinid_key);
		cacheMapByViewwinid.clear();
		for (ViewEvent viewevent : result)
		{
			cacheMapByPk.put(viewevent.getId() + "", viewevent);

			String viewpanelid = viewevent.getViewpanelid() + "";
			List<ViewEvent> viewpanelidlist = null;
			if (cacheMapByViewpanelid.containsKey(viewpanelid))
			{
				viewpanelidlist = (List<ViewEvent>) cacheMapByViewpanelid
						.get(viewpanelid);
			} else
			{
				viewpanelidlist = new ArrayList<ViewEvent>();
			}
			viewpanelidlist.add(viewevent);
			cacheMapByViewpanelid.put(viewpanelid, viewpanelidlist);

			String viewwinid = viewevent.getViewwinid() + "";
			List<ViewEvent> viewwinidlist = null;
			if (cacheMapByViewwinid.containsKey(viewwinid))
			{
				viewwinidlist = (List<ViewEvent>) cacheMapByViewwinid
						.get(viewwinid);
			} else
			{
				viewwinidlist = new ArrayList<ViewEvent>();
			}
			viewwinidlist.add(viewevent);
			cacheMapByViewwinid.put(viewwinid, viewwinidlist);
		}

		// 排序处理
		Set<String> viewPanelids = cacheMapByViewpanelid.keySet();
		for (String viewPanelid : viewPanelids)
		{
			List<ViewEvent> viewEvents = (List<ViewEvent>) cacheMapByViewpanelid
					.get(viewPanelid);
			sort(viewEvents);
			cacheMapByViewpanelid.put(viewPanelid, viewEvents);
		}

		List<ViewPanel> viewPanels = viewPanelDaoImpl.findAll();
		for (ViewPanel viewPanel : viewPanels)
		{
			String key = viewPanel.getId().toString();
			if (!cacheMapByViewpanelid.containsKey(key))
			{
				cacheMapByViewpanelid.put(key, ZONELIST);
			}
		}

		List<ViewWin> viewWins = viewWinDaoImpl.findAll();
		for (ViewWin viewWin : viewWins)
		{
			String key = viewWin.getId().toString();
			if (!cacheMapByViewwinid.containsKey(key))
			{
				cacheMapByViewwinid.put(key, ZONELIST);
			}
		}
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByPk_key, cacheMapByPk);
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByViewpanelid_key,
				cacheMapByViewpanelid);
		ViewCacheUtil.put2ConfigDataCacheMap(cacheMapByViewwinid_key,
				cacheMapByViewwinid);
		return result;
	}
}
